<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/9/19
 * Time: 14:42
 */

namespace app\admin\controller;


use think\Controller;
use app\admin\model\Tripcdk as M;
use think\Cookie;
use think\Db;

class Tripcdk extends Controller
{
    public function index(){
       $m= new  M();

        $post = $this->request->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $where='';
        $da='';
        if (isset($post['tripname']) and !empty($post['tripname'])) {
            $where['name'] = ['like', '%' . $post['tripname'] . '%'];
          $id=  Db::name('trip')->where($where)->field('id')->select();
          if (!empty($id)){
              foreach ($id as $v){
                  $da['trip_id'][]=['=',$v['id']];
              }
              $da['trip_id'][]='or';
          }
        }

      $da=$m->where($da)->order('tripcdk_id desc')->paginate(20);


$this->assign('dat',$da);
$this->assign('wherelist',$post);

        return $this->fetch();
    }


    //CDK生成
    public function addcdk(){
        if (request()->isPost()){
            $post=  request()->post();
            $res=[];
            Db::startTrans();
            try{
                db('tripcdk')->insert([
                    'trip_id'=>$post['trip_id'],
                    'member_id'=>!empty($post['member_id'])?$post['member_id']:0,
                    'uptime'=>time(),
                    'endtime'=>(strtotime($post['endtime'])+86399),
                    'admin_id'=>Cookie::get('admin'),
                    'CDK'=>$post['cdk'],
                ]);

                Db::commit();
                $res['msg']='操作成功';
                $res['code']=1;
            }catch (\Exception $e){
                Db::rollback();
                $res['msg']='操作失败';
                $res['code']=2;
                $res['error']=$e->getMessage();

            }


           return $res;
            die;
        }
    $trip= db('trip')->field('id,name')->select();
     $member=db('member')->field('member_id,member_nickname')->select();
$this->assign('member',$member);
$this->assign('trip',$trip);
return $this->fetch();

    }
//随机字符串
    public  function randomkeys(){
        $pattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key='';
    for($i=0;$i<6;$i++)
    {
        $key .= $pattern{mt_rand(0,35)};    //生成php随机数
    }
        return $key;
    }

    public function dcl(){
     $tripcdk_id=   input('id');
        $res='';
     if (!empty($tripcdk_id)){
         $da= db('tripcdk')->where('tripcdk_id',$tripcdk_id)->delete();
         if ($da){
             $res['msg']='操作成功';
             $res['code']=1;
         }else{
             $res['msg']='操作失败';
             $res['code']=2;
         }
         return $res;
     }else{
         $res['msg']='操作失败';
         $res['code']=2;
     }
        return $res;
    }

}


